import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { BookSource } from './source.entity';

@Injectable()
export class SourceService {
  constructor(
    @InjectRepository(BookSource)
    private readonly bookSourceRepository: Repository<BookSource>,
  ) {}

  async findAll(): Promise<BookSource[]> {
    return this.bookSourceRepository.find();
  }

  async findOne(id: number): Promise<BookSource> {
    return await this.bookSourceRepository.findOne({ where: { id } });
  }

  async create(data: Partial<BookSource>): Promise<BookSource> {
    const newSource = this.bookSourceRepository.create(data);
    return await this.bookSourceRepository.save(newSource);
  }

  async update(id: number, data: Partial<BookSource>): Promise<BookSource> {
    await this.bookSourceRepository.update(id, data);
    return this.findOne(id);
  }

  async remove(id: number): Promise<void> {
    await this.bookSourceRepository.delete(id);
  }
} 